package com.yuyang.wifi.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.yuyang.wifi.R;
import com.yuyang.wifi.utils.LogUtils;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * Created by liting on 17/2/19.
 */

public class MyProgressView extends AppCompatTextView {
    private boolean progressfillet;
    private int mProgress = -1;
    private String mText;
    private Paint mPaintProgressBg, mPaintProgress, mPaintText;
    Xfermode xfermode;
    private int mBgColor = getResources().getColor(R.color.soft),
            mProgressColor = getResources().getColor(R.color.white),
            mFontColor = getResources().getColor(R.color.white),
            mProgressColorBg = getResources().getColor(R.color.commen_999999),
            mRound = 0, mFontSize = 10;
    int mViewWidth;
    int mViewHeight;
    private boolean isHasProgress;
    private boolean isPause;

    public MyProgressView(Context context) {
        super(context);
    }


    public MyProgressView(Context context, AttributeSet set) {
        super(context, set);
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
        TypedArray array = context.obtainStyledAttributes(set, R.styleable.MyProgressView);
        mBgColor = array.getInt(R.styleable.MyProgressView_bgcolor, getResources().getColor(R.color.soft));
        mProgressColor = array.getInt(R.styleable.MyProgressView_progresscolor, getResources().getColor(R.color.white));
        mFontColor = array.getInt(R.styleable.MyProgressView_fontColor, getResources().getColor(R.color.white));
        mRound = array.getDimensionPixelSize(R.styleable.MyProgressView_round, 0);
        mFontSize = array.getDimensionPixelSize(R.styleable.MyProgressView_fontSize, 12);
        progressfillet = array.getBoolean(R.styleable.MyProgressView_progressfillet, false);
        mText = array.getString(R.styleable.MyProgressView_textcon);
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int mR = mRound;
        canvas.drawColor(Color.TRANSPARENT);

        //背景
        if (mPaintProgressBg == null) {
            mPaintProgressBg = new Paint();
        }
        mPaintProgressBg.setColor(mProgress > 0 ? mProgressColor : mBgColor);
        mPaintProgressBg.setStyle(Paint.Style.FILL);
        if (Build.VERSION.SDK_INT >= 21 && mR > 0) {
            canvas.drawRoundRect(0, 0, mViewWidth, mViewHeight, mR, mR, mPaintProgressBg);
        } else {
            canvas.drawRect(0, 0, mViewWidth, mViewHeight, mPaintProgressBg);
        }
        mPaintProgressBg.setXfermode(xfermode);

        //进度
        if (mProgress > -1) {
            if (mPaintProgress == null) {
                mPaintProgress = new Paint();
                mPaintProgress.setAntiAlias(true);// 抗锯齿效果
            }
            int width = (int) (mViewWidth * (mProgress * 1.0f / 100) + 0.5f);
            int saved = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
            mPaintProgress.setColor(mProgressColor);
            canvas.drawRoundRect(new RectF(0, 0, mViewWidth, mViewHeight), mR, mR, mPaintProgress);
            mPaintProgress.setXfermode(xfermode);
            mPaintProgress.setColor(mBgColor);
            mPaintProgress.setStyle(Paint.Style.FILL);
            //进度条样式
            if (Build.VERSION.SDK_INT >= 21) {
                if (progressfillet && mR > 0) {
                    canvas.drawRoundRect(new RectF(width - mViewWidth, 0, width, mViewHeight), mR, mR, mPaintProgress);
                } else {
                    canvas.drawRoundRect(new RectF(width - mViewWidth, 0, width, mViewHeight), 0, 0, mPaintProgress);
                }
            } else {
                canvas.drawRect(0, 0, width, mViewHeight, mPaintProgress);
            }

            if (isPause) {
                //下载暂停
            } else {
                mText = mProgress + "%";
            }
            mPaintProgress.setXfermode(null);
            canvas.restoreToCount(saved);
        }

        //text
        if (!TextUtils.isEmpty(mText)) {
            if (mPaintText == null) {
                mPaintText = new Paint();
                mPaintText.setFlags(Paint.ANTI_ALIAS_FLAG);
                mPaintText.setTextAlign(Paint.Align.LEFT);
                mPaintText.setTextSize(mFontSize);
                mPaintText.setColor(mFontColor);
            }
            float stringWidth = mPaintText.measureText(mText);
            float x = (getWidth() - stringWidth) / 2;
            Paint.FontMetrics fontMetrics = mPaintText.getFontMetrics();
            float y = getHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2;
            canvas.drawText(mText, x, y, mPaintText);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        mPaintProgressBg = null;
        mPaintProgress = null;
        mPaintText = null;
        super.onDetachedFromWindow();
    }

    public void setBtn(String txt) {
        mProgress = -1;
        mText = txt;
        invalidate();
    }


    public void setText(String txt) {
        setBtn(txt);
    }

    public void setProgress(int p) {
        isHasProgress = true;
        if (p == 0 || p != mProgress) {
            if (p >= 0) {
                isPause = false;
                LogUtils.e("progress", p + "");
                mProgress = p > 100 ? 100 : p <= 0 ? 0 : p;
                invalidate();
            }
        }
    }

    public void setIsPause(boolean pause, String strPause) {
        mText = strPause;
        isPause = pause;
        invalidate();
    }

    public void setIsPause(boolean pause, String strPause, int progress) {
        mProgress = progress;
        mText = strPause;
        isPause = pause;
        invalidate();
    }

    public String getPreStr() {
        return mText;
    }

    public boolean getIsHasProgress() {
        return isHasProgress;
    }

    public void setIsHasProgress() {
        isHasProgress = false;
    }
}
